﻿using System;
using System.Collections.Generic;
using System.Linq;

namespace ud_Article.Business
{
    public class DBArticleCategory
    {
        readonly LinqArticle db = new LinqArticle();

        public ud_ArticleCategory GetOne(int categoryID)
        {
            var result = from p in db.ud_ArticleCategories
                         where p.CategoryID.Equals(categoryID)
                         select p;
            return result.FirstOrDefault();
        }

        public ud_ArticleItemCategory GetArticleItemCategory(int itemID)
        {
            var result = from p in db.ud_ArticleItemCategories
                         where p.ItemID.Equals(itemID)
                         select p;
            return result.FirstOrDefault();
        }

        public List<ud_ArticleItemCategory> GetAllArticleItemCategory(int catID)
        {
            var result = from p in db.ud_ArticleItemCategories
                         where p.CategoryID.Equals(catID)
                         select p;
            return result.ToList();
        }

        public ud_ArticleCategory GetOneByArticleID(int itemID)
        {
            ud_ArticleItemCategory item = GetArticleItemCategory(itemID);
            ud_ArticleCategory result = new ud_ArticleCategory();
            if (item != null)
            {
                result = (from p in db.ud_ArticleCategories
                             where p.CategoryID.Equals(item.CategoryID)
                             select p).FirstOrDefault();
            }
            return result;
        }

        public List<ud_ArticleCategory> GetAll(int moduleID)
        {
            var result = from p in db.ud_ArticleCategories
                         where p.ModuleID.Equals(moduleID)
                         orderby p.SortOrder ascending, p.LastModified descending
                         select p;
            return result.ToList();
        }

        public List<ud_ArticleCategory> GetAll(int moduleID, int number)
        {
            var result = from p in db.ud_ArticleCategories
                         where p.ModuleID.Equals(moduleID)
                         orderby p.SortOrder ascending , p.LastModified descending
                         select p;
            if (result.Count() > number)
            {
                return result.Take(number).ToList();
            }
            return result.ToList();
        }

        public List<ud_ArticleCategory> GetChildren(int categoryId)
        {
            var result = (from p in db.ud_ArticleCategories
                         where p.ParentID.Equals(categoryId)
                         orderby p.SortOrder ascending, p.LastModified descending
                         select p).ToList();
            return result.ToList();
        }

        public ud_ArticleCategory GetParent(int categoryId)
        {
            ud_ArticleCategory item = GetOne(categoryId);
            ud_ArticleCategory result = new ud_ArticleCategory();
            if (item != null)
            {
                result = (from p in db.ud_ArticleCategories
                              where p.CategoryID == item.ParentID
                              select p).FirstOrDefault();
            }
            return result;
        }

        public void GetParentCategory(int categoryID, ref List<ud_ArticleCategory> listCategory)
        {
            ud_ArticleCategory parent = GetParent(categoryID);
            if (parent != null && parent.CategoryID > 0)
            {
                listCategory.Add(parent);
                GetParentCategory(parent.CategoryID, ref listCategory);
            }
        }

        public List<ud_ArticleCategory> GetRoot(int moduleID)
        {
            var result = from p in db.ud_ArticleCategories
                         where p.ModuleID.Equals(moduleID) && p.ParentID.Equals(0)
                         orderby p.SortOrder ascending, p.LastModified descending
                         select p;
            return result.ToList();
        }

        public bool Delete(int categoryID)
        {
            bool flag = false;
            List<ud_ArticleItemCategory> listItem = GetAllArticleItemCategory(categoryID);
            db.ud_ArticleItemCategories.DeleteAllOnSubmit(listItem);
            ud_ArticleCategory item = GetOne(categoryID);
            if (item != null && DeleteChildren(categoryID))
            {
                db.ud_ArticleCategories.DeleteOnSubmit(item);
            }
            try
            {
                db.SubmitChanges();
                flag = true;
            }
            catch
            { }
            return flag;
        }

        public bool DeleteChildren(int categoryID)
        {
            bool flag = false;
            List<ud_ArticleCategory> items = GetChildren(categoryID);
            foreach (var item in items)
            {
                Delete(item.CategoryID);
            }
            try
            {
                db.SubmitChanges();
                flag = true;
            }
            catch
            { }
            return flag;
        }

        public bool Create(int moduleID, string category, int parentID, int sortOrder, string imageUrl, string description)
        {
            bool flag = false;

            ud_ArticleCategory item = new ud_ArticleCategory
            {
                ModuleID = moduleID,
                Category = category,
                ParentID = parentID,
                SortOrder = sortOrder,
                LastModified = DateTime.UtcNow,
                ImageUrl = imageUrl,
                Description = description
            };
            db.ud_ArticleCategories.InsertOnSubmit(item);
            try
            {
                db.SubmitChanges();
                flag = true;
            }
            catch { }
            return flag;
        }

        public bool Update(int categoryID, string category, int parentID, int sortOrder, string imageUrl, string description)
        {
            bool flag = false;
            ud_ArticleCategory item = GetOne(categoryID);
            if (item != null)
            {
                item.Category = category;
                item.ParentID = parentID;
                item.SortOrder = sortOrder;
                item.LastModified = DateTime.UtcNow;
                item.ImageUrl = imageUrl;
                item.Description = description;
                try
                {
                    db.SubmitChanges();
                    flag = true;
                }
                catch { }
            }
            return flag;
        }

        public void DeleteByModule(int moduleID)
        {
            var result = from p in db.ud_ArticleCategories
                         where p.ModuleID.Equals(moduleID)
                         select p;
            db.ud_ArticleCategories.DeleteAllOnSubmit(result);
            db.SubmitChanges();
        }
    }
}
